package com.leyou.page.service.impl;

import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.page.service.GoodsPageService;
import com.leyou.page.vo.SpecGroupVO;
import com.leyou.page.vo.SpecParamVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsPageServiceImpl implements GoodsPageService {
    @Autowired
    ItemClient itemClient;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    private static final String KEY_PREFIX_SPU = "page:spu:id:";
    private static final String KEY_PREFIX_SKU = "page:sku:id:";
    private static final String KEY_PREFIX_DETAIL = "page:detail:id:";
    private static final String KEY_PREFIX_CATEGORY = "page:category:id:";
    private static final String KEY_PREFIX_BRAND = "page:brand:id:";
    private static final String KEY_PREFIX_SPEC = "page:spec:id:";
    @Override
    public String loadSpuData(Long spuId) {
        //查询出spu
        SpuDTO spuDTO = itemClient.querySpuById(spuId);
        //健壮性判断
        if (spuDTO==null||spuDTO.getId()==null){
            //说明商品不存在
            throw new LyException(404,"商品不存在");
        }
        //spu存在转换json存放redis
        //先转换成map
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",spuDTO.getId());
        map.put("name",spuDTO.getName());
        map.put("categoryIds",spuDTO.getCategoryIds());
        map.put("brandId",spuDTO.getBrandId());
        //使用工具类转换
        String json = JsonUtils.toJson(map);
        //写入缓存
        stringRedisTemplate.opsForValue().set(KEY_PREFIX_SPU+spuId,json);
        return json;
    }

    @Override
    public String loadSpuDetailData(Long spuId) {
        //查询spudetail
        SpuDetailDTO spuDetailDTO = itemClient.querySpuDetailById(spuId);
       //转换json
        String json = JsonUtils.toJson(spuDetailDTO);
        //放到缓存
        stringRedisTemplate.opsForValue().set(KEY_PREFIX_DETAIL+spuId,json);
        return json;
    }

    @Override
    public String loadSkuListData(Long spuId) {
        //查询
        List<SkuDTO> skuDTOS = itemClient.querySkuBySpuId(spuId);
        //转换
        String json = JsonUtils.toJson(skuDTOS);
        //写到缓存
        stringRedisTemplate.opsForValue().set(KEY_PREFIX_SKU+spuId,json);
        return json;
    }

    @Override
    public String loadCategoriesData(List<Long> ids) {
        //查询
        List<CategoryDTO> categoryDTOS = itemClient.queryCategoryByIds(ids);
        //转换vo
        List<HashMap<String, Object>> collect = categoryDTOS.stream().map(categoryDTO -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", categoryDTO.getId());
            map.put("name", categoryDTO.getName());
            return map;
        }).collect(Collectors.toList());
        //转换json
        String json = JsonUtils.toJson(collect);
        //写缓存 key需要ids.get(2)三级目录
        stringRedisTemplate.opsForValue().set(KEY_PREFIX_CATEGORY+ids.get(2),json);
        return json;
    }

    @Override
    public String loadBrandData(Long brandId) {
        //查询
        BrandDTO brandDTO = itemClient.queryById(brandId);
        //转换vo
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",brandDTO.getId());
        map.put("name",brandDTO.getName());
        //转换json
        String json = JsonUtils.toJson(map);
        //放缓存
        stringRedisTemplate.opsForValue().set(KEY_PREFIX_BRAND+brandId,json);
        return json;
    }

    @Override
    public Boolean deleteSku(Long spuId) {
        return stringRedisTemplate.delete(KEY_PREFIX_SKU + spuId);
    }

    @Override
    public String loadSpecData(Long categoryId) {
        //查询
        List<SpecGroupDTO> specGroupDTOS = itemClient.querySpecList(categoryId);
        //转换vo
        ArrayList<SpecGroupVO> objects = new ArrayList<>();
        for (SpecGroupDTO specGroupDTO : specGroupDTOS) {
            SpecGroupVO specGroupVO = new SpecGroupVO();
            specGroupVO.setName(specGroupDTO.getName());
            //查询的结果是dto需要转换 类型是specparam
            specGroupVO.setParams(BeanHelper.copyWithCollection(specGroupDTO.getParams(),SpecParamVO.class));
            objects.add(specGroupVO);
        }
        //转换json
        String json = JsonUtils.toJson(objects);
        //放到缓存
        stringRedisTemplate.opsForValue().set(KEY_PREFIX_SPEC+categoryId,json);
        return json;
    }
}
